﻿using System;
using System.Windows.Data;
using System.Globalization;

namespace Red.Controls.Converters
{
    public class ValueMultiplierConverter : IValueConverter, IMultiValueConverter
    {
        #region IValueConverter

        public object Convert( object valueObject, Type targetType, object parameter, CultureInfo culture )
        {
            string multiplierStr = (string)parameter;
            double muliplier = double.Parse( multiplierStr, CultureInfo.InvariantCulture );

            double value = (double)valueObject;
            return value * muliplier;
        }

        public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
        {
            throw new NotImplementedException();
        }

        #endregion IValueConverter

        #region IMultiValueConverter

        public object Convert( object[] values, Type targetType, object parameter, CultureInfo culture )
        {
            double result = 1.0;

            foreach ( double value in values )
            {
                result *= value;
            }

            if ( double.IsNaN( result ) )
            {
                return 0.0;
            }

            return result;
        }

        public object[] ConvertBack( object value, Type[] targetTypes, object parameter, CultureInfo culture )
        {
            throw new Exception( "Not implemented" );
        }

        #endregion IMultiValueConverter
    }
}
